{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b261d84d-ae98-4b31-be7e-4f380a4a5a78",
   "metadata": {},
   "source": [
    "# Property Graph Index Visualization\n",
    "\n",
    "Similar to the [property_graph_basic](property_graph_basic.ipynb) notebook, in this notebook, we demonstrate an alternative visualization approach for the default ```SimplePropertyGraphStore```\n",
    "\n",
    "While the focus of the other notebook is querying the graph, this notebook focuses on the visualization aspect of what was created."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15a252e2-8cf8-4202-a561-8baa74c3393a",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e30c1c6-3d95-435d-beb7-30546d344e14",
   "metadata": {},
   "source": [
    "## Setup "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee7179d1-b8a8-403e-8541-0d26fed5ae92",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import urllib.request\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"sk-proj-...\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc8ba66e-9561-4868-9018-682710d6f666",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt\"\n",
    "filename = \"data/paul_graham/paul_graham_essay.txt\"\n",
    "os.makedirs(os.path.dirname(filename), exist_ok=True)\n",
    "urllib.request.urlretrieve(url, filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ce1e3e5-d14b-4403-9a75-19df04b3132b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import nest_asyncio\n",
    "\n",
    "nest_asyncio.apply()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "568cddc4-ba5d-4035-abf6-39a7520fedec",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core import SimpleDirectoryReader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28c443c0-3803-4387-b3fa-09aa2c6406c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "documents = SimpleDirectoryReader(\"./data/paul_graham/\").load_data()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e44a55e-1365-4bb8-91de-42b5ff8e90a4",
   "metadata": {},
   "source": [
    "## Construction "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f1ea68f-810f-4175-bb93-9bc28fc8cf92",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core import PropertyGraphIndex\n",
    "from llama_index.embeddings.openai import OpenAIEmbedding\n",
    "from llama_index.llms.openai import OpenAI\n",
    "\n",
    "index = PropertyGraphIndex.from_documents(\n",
    "    documents,\n",
    "    llm=OpenAI(model=\"gpt-3.5-turbo\", temperature=0.3),\n",
    "    embed_model=OpenAIEmbedding(model_name=\"text-embedding-3-small\"),\n",
    "    show_progress=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "154c8f70-1901-445f-b3b5-7031210fc919",
   "metadata": {},
   "source": [
    "## Visualization\n",
    "\n",
    "Let's explore what we created. Using the ```show_jupyter_graph()``` method to create our graph directly in the Jupyter cell!\n",
    "\n",
    "Note that this only works in Jupyter environments."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aea6f724-4820-45f9-bea1-fe6cf87d2e1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "index.property_graph_store.show_jupyter_graph()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b94b167-248c-49aa-883e-e289438cd1b6",
   "metadata": {},
   "source": [
    "![example graph](./jupyter_screenshot.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
